package ip.command;
/***********************************************************************
 * Module:  CreateCountryCommand.java
 * Author:  mojsilo
 * Purpose: Defines the Class CreateCountryCommand
 ***********************************************************************/

import ip.dto.CountryDTO;
import ip.ejb.session.facade.CountryFacadeLocal;
import ip.ejb.session.facade.CountryFacadeLocalHome;
import ip.exceptions.CanNotCreateNewCountryException;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public class CreateCountryCommand extends Command
{
	private CountryDTO countryDTO;
	private CountryFacadeLocal countryFacadeLocal;
	private Logger logger = Logger.getLogger(this.getClass());

	public void init() throws UnexpectedException{
		try {
			logger.debug("init()");
			CountryFacadeLocalHome countryFacadeLocalHome = (CountryFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.CountryFacadeLocalHome.class);
			countryFacadeLocal = countryFacadeLocalHome.create();
			
		} catch (NamingException e) {
			e.printStackTrace();
			logger.error(e);
			throw new UnexpectedException();			
		} catch (CreateException e) {
			e.printStackTrace();
			logger.error(e);
			throw new UnexpectedException();			
		}

	}

	public void execute() throws UnexpectedException,CanNotCreateNewCountryException{
		try {
			logger.debug("execute()");
			countryFacadeLocal.createCountry(countryDTO);
		} catch (CanNotCreateNewCountryException e) {
			logger.error(e);
			e.printStackTrace();
			throw new CanNotCreateNewCountryException(countryDTO);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new UnexpectedException();			
		}
	}

	public void dispose() throws UnexpectedException{
		try {
			logger.debug("dispose()");
			countryFacadeLocal.remove();

		} catch (EJBException e) {					
			e.printStackTrace();
			logger.error(e);
			throw new UnexpectedException();
		} catch (RemoveException e) {		
			e.printStackTrace();
			logger.error(e);
			throw new UnexpectedException();
		}
	}

	public void setCountryDTO(CountryDTO countryDTO)
	{
		this.countryDTO = countryDTO;
	}

}